package com.beibojd.service.impl;

import com.beibojd.dao.CustomerMapper;
import com.beibojd.dao.OrderAddMapper;
import com.beibojd.dao.OrderMapper;
import com.beibojd.pojo.Customer;
import com.beibojd.pojo.Order;
import com.beibojd.pojo.OrderAdd;
import com.beibojd.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: 洪耀荣
 * @Date: 2021-06-26 08:37:09
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderAddMapper orderAddMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Order> selectAll() {
        return orderMapper.selectAll();
    }

    @Override
    public List<Order> selectByOrderId(Integer id) {
        return orderMapper.selectById(id);
    }

    @Override
    public List<Order> selectByExample(Order order) {
        Example example= createExampleOrder(order);
        return orderMapper.selectByExample(example);
    }

    @Override
    public Example createExampleOrder(Order order) {
        Example example=new Example(Order.class);
        Example.Criteria criteria=example.createCriteria();
        if (order!=null){
            if (!StringUtils.isEmpty(order.getOrderId())){
                criteria.andEqualTo("orderId",order.getOrderId());
            }
            if (!StringUtils.isEmpty(order.getCustomerName())){
                criteria.andEqualTo("customerName");
            }
            if (!StringUtils.isEmpty(order.getGoodsname())){
                criteria.andEqualTo("goodsName");
            }
        }
        return example;
    }

    /**
     * 创建订单：
     * 获取前端输入的客户名与运输方式
     * 去order表找上一个订单号
     * 根据客户名找客户id
     * 打包成order类型，存入order表
     * @param customerName
     * @param transport
     * @return
     */
    @Override
    public List<Order> addOrder(String customerName, String transport) {
        List<Customer> customers=customerMapper.selectOneByName(customerName);
        //客户数据获取补全
        Customer customer= customers.get(0);
        Integer customerId=customer.getCustomerId();
        if (transport.equals("")){
            String defaultCustomerTransport=customer.getCustomerTransport();
            transport=defaultCustomerTransport;
        }
        //日期处理
        Date date=new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format.format(date);
        //获取订单编号
        Integer orderId=orderMapper.maxOrder()+1;
        List<OrderAdd> adds=orderAddMapper.selectAll();
        double totalPrice=0;
        //类型转换，开始保存
        for (OrderAdd orderadd :adds){
            Order order=new Order(orderId,customerId,transport,customerName,
                    orderadd.getGoodsName(),orderadd.getGoodsFactory(),
                    orderadd.getGoodsModel(),orderadd.getGoodsSpecs(),
                    orderadd.getGoodsNum(),orderadd.getGoodsPrice(),
                    orderadd.getGoodsUnit(),orderadd.getRemarks(),dateStr);
            totalPrice+=orderadd.getGoodsPrice()*orderadd.getGoodsNum();
            orderMapper.insert(order);
        }
        //删除临时表
        orderAddMapper.deleteAll();
        List<Order> orders=orderMapper.selectById(orderId);
        return orders;
    }
}
